package com.tekj.cxqcb.view.Module.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.UploadFileBean;
import com.tekj.cxqcb.view.Module.activity.StoreModule.ModifyStoreAddressActivity;
import com.tekj.cxqcb.view.Module.activity.PoiAroundSearchActivity;
import com.tekj.cxqcb.view.Module.bean.UpCarShopAddressBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.fragment.BaseFragment;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static android.app.Activity.RESULT_OK;


public class ModifyAddressFragment extends BaseFragment {

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.et_address3)
    EditText etAddress3;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;

    private BusinessDao businessDao;
    private String imgeString;
    private PoiItem poiItem;
    private double latitude, longitude;
    private String province, city, region, address, houseNumber, name;
    private ModifyStoreAddressActivity addressActivity;

    @Override
    public void init() {
//        new CityNameSet(activity,tvAddress,1);
        addressActivity = (ModifyStoreAddressActivity) getActivity();
        EventBus.getDefault().register(this);
        businessDao = new BusinessDao(activity, this);
        initData();
    }

    @Override
    public int setLayout() {
        return R.layout.fragment_modify_address;
    }

    @Override
    public void initLoadStart() {
        activity.showDialog();
    }

    @Override
    public void initLoadEnd() {
        activity.dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 单文件上传:
                UploadFileBean statusBean = (UploadFileBean) bindingViewBean.getBean();
                if ("0000000".equals(statusBean.getCode())) {
                    imgeString = statusBean.getData().getUploadFilePath();
                } else {
                    Toasty.normal(activity, statusBean.getMsg()).show();
                }

                break;
            case 修改门店:
                PubStatusBean pubStatusBean= (PubStatusBean) bindingViewBean.getBean();
                if (pubStatusBean.getCode().equals("0000000")){
                    activity.finish();
                }else {
                Toasty.normal(activity,pubStatusBean.getMsg()).show();
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void Event(EventBusMsgBean e) {
        switch (e.getmEnumStatus()) {
            case 提交门店地址:
                int type = (int) e.getObject();
                if (type == 0) {
                    UpCarShopAddress();
                }
                break;

            case 门店地址图片:
                LocalMedia imge = (LocalMedia) e.getObject();
                Picasso.get().load(new File(imge.getCompressPath()))
                        .into(imgPhoto);
                businessDao.UploadFile(imge.getCompressPath());
                break;
        }
    }

    @OnClick({R.id.tv_address, R.id.img_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_address:
                startActivityForResult(new Intent(activity, PoiAroundSearchActivity.class), 101);
                break;
            case R.id.img_photo:
                Tools.InitPictureSelector(getActivity(), false, 102);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 102:
                    LocalMedia imge = PictureSelector.obtainMultipleResult(data).get(0);
                    Picasso.get().load(new File(imge.getCompressPath()))
                            .into(imgPhoto);
                    businessDao.UploadFile(imge.getCompressPath());
                    break;
                case 101:
                    poiItem = data.getParcelableExtra("poidata");

                    province = poiItem.getProvinceCode();
                    city = poiItem.getCityCode();
                    region = poiItem.getAdCode();
                    address = poiItem.getSnippet();
                    Toasty.normal(activity, poiItem.getCityName() + city).show();
                    latitude = poiItem.getLatLonPoint().getLatitude();
                    longitude = poiItem.getLatLonPoint().getLongitude();

                    tvAddress.setText(address);
                    break;
            }
        }
    }

    /**
     * 初始化一下各个参数数据
     */
    void initData() {
        if (addressActivity.dataBean==null)
            return;
        if (addressActivity.dataBean.getType() == 2) {
            return;
        }

        name = addressActivity.dataBean.getShopName();

        province = addressActivity.dataBean.getProvince();
        city = addressActivity.dataBean.getCity();
        region = addressActivity.dataBean.getRegion();

        address = addressActivity.dataBean.getAddress();
        houseNumber = addressActivity.dataBean.getHouseNumber();
        imgeString = addressActivity.dataBean.getShopImage();

        latitude = addressActivity.dataBean.getLatitude();
        longitude = addressActivity.dataBean.getLongitude();

        SetViewData();
    }

    /**
     *
     */
    void SetViewData() {

        etName.setText(name);
        etAddress3.setText(houseNumber);
        tvAddress.setText(address);
        Picasso.get().load(imgeString)
                .into(imgPhoto);


    }

    /**
     * 提交私人地址修改
     */
    void UpCarShopAddress() {
        final UpCarShopAddressBean bean = new UpCarShopAddressBean();

        name = etName.getText().toString();
        houseNumber = etAddress3.getText().toString().trim();
        if (name.equals("")) {
            Toasty.normal(activity, "请填入提车门店名称").show();
            return;
        }
        bean.setType("1");
        bean.setShopName(name);
        bean.setOldType(addressActivity.dataBean.getType());

        bean.setProvince(province);
        bean.setCity(city);
        bean.setRegion(region);

        bean.setAddress(address);

        bean.setLatitude(latitude);
        bean.setLongitude(longitude);


        bean.setHouseNumber(houseNumber);
        bean.setBusinessId(MainBActivity.User.getUserId());

        bean.setShopImage(imgeString);

        Tools.showNormalDialogOne(activity, "修改门店", "确定修改？", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                businessDao.UpdateUserTakeCarShop(bean);
            }
        }, null);


    }
}
